# -*- coding: utf-8 -*-
"""
数学公式场景模板 - 专门用于数学公式展示
"""

from typing import Any, Dict, Optional
from pathlib import Path

from .base_template import BaseSceneTemplate


class MathSceneTemplate(BaseSceneTemplate):
    """数学公式场景模板"""

    def __init__(
        self,
        title: str = "",
        formula: str = "",
        explanation: str = "",
        audio_duration: float = 0.0,
        subtitle_file: Optional[Path] = None,
        theme=None,
    ):
        """
        初始化数学场景

        Args:
            title: 标题
            formula: 数学公式（LaTeX格式）
            explanation: 公式说明
            audio_duration: 音频时长
            subtitle_file: 字幕文件
            theme: 主题
        """
        super().__init__(title, explanation, audio_duration, subtitle_file, theme)
        self.formula = formula
        self.explanation = explanation

    def create_content(self, scene) -> Dict[str, Any]:
        """
        创建数学内容

        Args:
            scene: Manim场景对象

        Returns:
            包含标题、公式和说明的字典
        """
        content = {}

        # 创建标题
        if self.title:
            title_style = self.get_style("title")
            title_obj = scene.Text(
                self.title,
                font_size=title_style["font_size"],
                color=title_style["color"],
                font=title_style["font"],
            )
            title_obj.to_edge(scene.UP, buff=self.get_position("safe_margin_y"))
            content["title"] = title_obj

        # 创建公式
        if self.formula:
            formula_style = self.get_style("math")
            formula_obj = scene.MathTex(
                self.formula,
                color=formula_style["color"],
            )
            # 居中显示公式
            formula_obj.center()
            content["formula"] = formula_obj

        # 创建说明
        if self.explanation:
            explanation_style = self.get_style("content")
            explanation_obj = scene.Text(
                self.explanation,
                font_size=explanation_style["font_size"],
                color=explanation_style["color"],
                font=explanation_style["font"],
                line_spacing=1.5,
            )
            # 放在公式下方
            if "formula" in content:
                explanation_obj.next_to(content["formula"], scene.DOWN, buff=0.5)
            else:
                explanation_obj.center()
            content["explanation"] = explanation_obj

        return content

    def animate_content(self, scene, content: Dict[str, Any]) -> None:
        """
        动画数学内容

        Args:
            scene: Manim场景对象
            content: 内容字典

        Returns:
            None
        """
        animations = []

        # 标题动画
        if "title" in content:
            title = content["title"]
            animations.append(scene.FadeIn(title, run_time=0.5))

        # 公式动画
        if "formula" in content:
            formula = content["formula"]
            # 数学公式的书写动画
            animations.append(scene.Write(formula, run_time=self.get_animation_config("write_duration")))

        # 说明动画
        if "explanation" in content:
            explanation = content["explanation"]
            animations.append(scene.FadeIn(explanation, run_time=0.5))

        # 播放所有动画
        for anim in animations:
            scene.play(anim)

            # 如果使用了TimeScaler，等待相应时间
            if self.scaler:
                self.scaler.current_time += anim.run_time
